package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserInfoMerge;
import com.koron.css2.clientCenter.bean.UserInfoMergeDetail;
import com.koron.css2.clientCenter.mapper.UserInfoMergeDetailMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMergeMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoMergeQuery;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 更新并户及并户详情数据
 *
 * @author 朱超
 * @since 2021-12-27 10:42:05
 */
public class UserInfoMergeUpdate implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoMergeMapper userInfoMergeMapper = factory.getMapper(UserInfoMergeMapper.class);
            UserInfoMergeDetailMapper userInfoMergeDetailMapper = factory.getMapper(UserInfoMergeDetailMapper.class);

            // 1. 根据请求json获取实例bean
            UserInfoMerge userInfoMerge = JsonUtils.objectToPojo(req.getData(), UserInfoMerge.class);
            List<UserInfoMergeDetail> userInfoMergeDetailList = userInfoMerge.getUserInfoMergeDetailList();
            // 1.1 根据id查询billNo
            UserInfoMergeQuery userInfoMergeQuery = new UserInfoMergeQuery();
            userInfoMergeQuery.setId(userInfoMerge.getId());
            UserInfoMerge fullUserInfoMerge = userInfoMergeMapper.getSingle(userInfoMergeQuery);
            String billNo = fullUserInfoMerge.getBillNo();
            // 2. 设置updateTime,updateAccount,updateName
            userInfoMerge.setUpdateInfo(userInfo);
            // 3. 根据code删除原有的userInfoMergeDetail
            userInfoMergeDetailMapper.deleteByMergeCode(billNo);
            // 4. 新增并户详情数据；更新并户mainCtmCode,mainCtmName
            for (UserInfoMergeDetail userInfoMergeDetail : userInfoMergeDetailList) {
                // 4.1 设置id，createAccount,createTime,createName
                userInfoMergeDetail.setCreateInfo(userInfo);
                // 4.2 mergeCode
                userInfoMergeDetail.setMergeCode(billNo);
                // 4.3 插入并户详情数据
                userInfoMergeDetailMapper.insert(userInfoMergeDetail);
                // 4.4 更新mainCtmCode,mainCtmName
                if (userInfoMergeDetail.getMainCtmFlag().equals(1)) {
                    userInfoMerge.setMainCtmCode(userInfoMergeDetail.getCtmNo());
                    userInfoMerge.setMainCtmName(userInfoMergeDetail.getCtmName());
                    userInfoMergeMapper.update(userInfoMerge);
                }
            }
            // 5. 返回结果
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "并户更新成功", void.class);
        } catch (Exception e) {
            logger.error("并户更新失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "并户更新失败", void.class);
        }

    }
}
